package com.example.as_sum.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.as_sum.R;
import com.example.as_sum.entity.ShopInfo;
import com.example.as_sum.event.ShopClickEvent;
import com.example.as_sum.event.ShopCollectEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ran on 2016/11/21.
 */

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.MyHodlerView> {

    private List<ShopInfo> mShopInfos;
    private Context mContext;
    private boolean isAllShow;
    public MyRecycleViewAdapter(Context context, List<ShopInfo> shopInfos) {
        mContext = context;
        mShopInfos = shopInfos;
        this.isAllShow=false;
    }
    public void setAllShow(boolean allShow) {
        isAllShow = allShow;
        notifyDataSetChanged();
    }
    @Override
    public MyHodlerView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_test, parent, false);
        return new MyHodlerView(view);
    }

    @Override
    public void onBindViewHolder(MyHodlerView holder, int position, List<Object> payloads) {
        if(payloads.isEmpty()){
            bindData(holder,position);
        }else{
            Log.e("----->","");
            ShopInfo shop=(ShopInfo) payloads.get(0);
            holder.mItemImgScNum.setText(shop.getCount()+"");
            if(shop.isCollect()){
                holder.mItemImgSc.setImageResource(R.drawable.sc);
            }else{
                holder.mItemImgSc.setImageResource(R.drawable.sc_true);
            }
        }
    }

    @Override
    public void onBindViewHolder(MyHodlerView holder, int position) {
       bindData(holder,position);
    }
    private void bindData(final MyHodlerView holder, final int position){
        final ShopInfo shop=mShopInfos.get(position);
        if(isAllShow){
            holder.mItemCheck.setVisibility(View.VISIBLE);
            if(shop.isState()){
                holder.mItemCheck.setImageResource(R.mipmap.check_able);
            }else{
                holder.mItemCheck.setImageResource(R.mipmap.check_unable);
            }
        }else{
            holder.mItemCheck.setVisibility(View.GONE);
        }
        if(shop.isCollect()){
            holder.mItemImgSc.setImageResource(R.drawable.sc);
        }else{
            holder.mItemImgSc.setImageResource(R.drawable.sc_true);
        }
        holder.mItemTitle.setText(shop.getSname());
        holder.mItemImgScNum.setText(shop.getCount()+"");
        holder.mItemCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(shop.isState()){
                    shop.setState(false);
                    holder.mItemCheck.setImageResource(R.mipmap.check_unable);
                    EventBus.getDefault().post(new ShopClickEvent(String.valueOf(shop.getSid()),false));
                }else{
                    shop.setState(true);
                    holder.mItemCheck.setImageResource(R.mipmap.check_able);
                    EventBus.getDefault().post(new ShopClickEvent(String.valueOf(shop.getSid()),true));
                }
            }
        });
        holder.mItemImgSc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(shop.isCollect()){
                    //取消收藏
//                    shop.setCollect(false);
//                    shop.setCount(Integer.valueOf(shop.getCount())-1);
//                    holder.mItemImgScNum.setText(shop.getCount()+"");
//                    holder.mItemImgSc.setImageResource(R.drawable.sc);
                    EventBus.getDefault().post(new ShopCollectEvent(position,false));
                }else{
                    //收藏
//                    shop.setCollect(true);
//                    holder.mItemImgSc.setImageResource(R.drawable.sc2);
//                    shop.setCount(Integer.valueOf(shop.getCount())+1);
//                    holder.mItemImgScNum.setText(shop.getCount()+"");
                    EventBus.getDefault().post(new ShopCollectEvent(position,true));
                }

            }
        });
    }
    @Override
    public int getItemCount() {
        return mShopInfos.size();
    }

    class MyHodlerView extends RecyclerView.ViewHolder {
        @Bind(R.id.item_check)
        ImageView mItemCheck;
        @Bind(R.id.item_title)
        TextView mItemTitle;
        @Bind(R.id.item_img_sc)
        ImageView mItemImgSc;
        @Bind(R.id.item_img_sc_num)
        TextView mItemImgScNum;
        public MyHodlerView(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
